package com.activiti.flow.controller;

import com.activiti.basic.model.ResponseUtil;
import com.activiti.basic.model.WebResponse;
import com.activiti.common.RestServiceController;
import com.activiti.flow.model.vo.ModelVo;
import com.activiti.flow.service.ModelService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 模型管理
 * Created  2019/4/23.
 * @author beikei
 *
 */
@Api(tags = "模型管理")
@RestController
@RequestMapping("/model")
public class ModelerController implements RestServiceController<Model, String> {

    @Autowired
    RepositoryService repositoryService;
    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    private ModelService modelService;

    @ApiOperation("新建模型")
    @PostMapping("newModel")
    public WebResponse<Model> newModel(ModelVo vo) throws Exception {
        Model model;
        if(StringUtils.isNotBlank(vo.getId())){
            model = modelService.updateModel(vo);
        }else {
            model = modelService.createModel(vo);
        }
       return ResponseUtil.buildResult().redirectUrl("/editor?modelId=" + model.getId());
    }

    @ApiOperation("发布模型")
    @PostMapping("{modelId}/deployment")
    public WebResponse<Model> deploy(@PathVariable("modelId") String modelId) throws Exception {
        Model model = modelService.deployModel(modelId);
        return ResponseUtil.success(model, "发布成功");
    }

    @ApiOperation("查询模型")
    @Override
    public Object getOne(@PathVariable("id") String id) {
        Model model = repositoryService.createModelQuery().modelId(id).singleResult();
        return ResponseUtil.success(model);
    }

    @ApiOperation("分页查询模型")
    @GetMapping("/list1")
    @Override
    public Object getList(@RequestParam(value = "rowSize", defaultValue = "1000", required = false) Integer rowSize, @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        List<Model> list = repositoryService.createModelQuery().listPage(rowSize * (page - 1)
                , rowSize);
        long count = repositoryService.createModelQuery().count();

        // 暂时未分页
        return list;
    }

    @ApiOperation("删除模型")
    @Override
    public Object deleteOne(@PathVariable("id") String id) {
        repositoryService.deleteModel(id);
        return ResponseUtil.buildResult().refresh();
    }

    @Override
    public Object postOne(@RequestBody Model entity) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Object putOne(@PathVariable("id") String s, @RequestBody Model entity) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Object patchOne(@PathVariable("id") String s, @RequestBody Model entity) {
        throw new UnsupportedOperationException();
    }


}
